package one.stand.zuul.config;

import one.stand.zuul.util.DingTalkMsgClient;
import one.stand.zuul.util.ErrorMsgTemplate;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.scheduling.annotation.Async;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

public class ErrorReporter {
    private DingTalkMsgClient dingTalkMsgClient = new DingTalkMsgClient();

    private static String LOCAL_IP;

    public ErrorReporter() {
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            LOCAL_IP = ip4.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    @Async("asyncServiceExecutor")
    public void send(ErrorMsgTemplate errorMsgTemplate) {
        errorMsgTemplate.setTime(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(new Date()));
        errorMsgTemplate.setHost(LOCAL_IP);
        dingTalkMsgClient.sendError(null, errorMsgTemplate);
    }
}
